layui.define(['jquery','laytpl', 'layer'], function (exports) {
    var $ = layui.jquery,
        app = layui.data('app'),
        laytpl = layui.laytpl,
        toolbar = 'toolbar';
    tablebtn = {};

    /**
     * 初始化表头按钮
     */
    tablebtn.init = function (option) {
        var data=option;
        var functionTpl = option.temp.html();
        var functionHtml = '.tpl-button',
            button = '.layui-btn',
            head = '.layui-card-header',
            btnHtml;
        laytpl(functionTpl).render(data, function (html) {
            //拼接按钮
            if(option.type==1) {
                $(functionHtml).html(html);
                //给按钮绑定触发事件
                $(functionHtml).find(button).on({
                    click: function () {
                        var funThis = $(this);
                        var callback = {
                            elem: funThis,
                            event: funThis.attr('lay-event')
                        };
                        layui.event(toolbar, 'click(*)', callback);
                    }
                });
            }else{
                //直接返回html
                btnHtml=html;
            }
        });

        $(head).on('click', '.search_btn', function () {//查询按钮点击
            var oThis = $(this),
                params = {
                    elem: oThis,
                    event: oThis.attr('lay-event'),
                    data: oThis.attr('lay-data')
                };
            layui.event(toolbar, 'click(*)', params);
        });
        return btnHtml;
    };

    /**
     * 绑定表头按钮事件
     */
    tablebtn.buildOnClick = function (option) {
        //初始化化按钮
        // var function_option = {functions:$.extend(true,functionOption.functions,option.functions),row:functionOption.row};//$.extend(true,option.row,functionOption.row)};
        var btnHtml = this.init(option);
        //定义按钮事件
        var _option = {
            getEvent: function (data) {
                console.log(data);
                console.log("getevent");
            },//查看
            editEvent: function (data) {
                console.log(data);
                console.log("editevent");
            },//修改
            addEvent: function (data) {
                console.log(data);
                console.log("addevent");
            },//新增
            deleteEvent: function (data) {
                console.log(data);
                console.log("deleteevent");
            },//删除
            importEvent: function (data) {
                console.log("importevent");
            },//导入
            exportEvent: function (data) {
                console.log("exportevent");
            },//导出
            disableEvent: function (data) {
                console.log("disableevent");
            },//禁用
            enableEvent: function (data) {
                console.log("enableevent");
            },//启用
            searchEvent: function (data) {
                console.log("searchevent");
            },//查询
            lineMoreEvent: function (data) {
                console.log("lineevent");
            },//行更多
            confirmEvent:function(data){
                console.log("confirmEvent");
            },//确认
            auditEvent: function (data) {
                console.log("auditEvent");
            },//审核
            accessoryEvent: function (data) {
                console.log("accessoryEvent");
            },//附件管理
            generateBhd: function (data) {
                console.log("generateBhd");
            },//生成补货单
            cancelBh: function (data) {
                console.log("cancelBh");
            },//取消补货
        }

        _option = $.extend(true, _option, option.event);
        if (_option) {
            layui.onevent(toolbar, 'click', function (data) {
                var event = data.event;
                switch (event) {
                    case 'getEvent':
                        _option.getEvent(data);
                        break;
                    case 'addEvent':
                        _option.addEvent(data);
                        break;
                    case 'editEvent':
                        _option.editEvent(data);
                        break;
                    case 'deleteEvent':
                        _option.deleteEvent(data);
                        break;
                    case 'importEvent':
                        _option.importEvent(data);
                        break;
                    case 'exportEvent':
                        _option.exportEvent(data);
                        break;
                    case 'disableEvent':
                        _option.disableEvent(data);
                        break;
                    case 'enableEvent':
                        _option.enableEvent(data);
                        break;
                    case 'searchEvent':
                        _option.searchEvent(data);
                        break;
                    case 'lineMoreEvent':
                        _option.lineMoreEvent(data);
                        break;
                    case 'confirmEvent':
                        _option.confirmEvent(data);
                        break;
                    case 'auditEvent':
                        _option.auditEvent(data);
                        break;
                    case 'accessoryEvent':
                        _option.accessoryEvent(data);
                        break;
                    case 'generateBhd':
                        _option.generateBhd(data);
                        break;
                    case 'cancelBh':
                        _option.cancelBh(data);
                        break;
                }
            });
        }
        return btnHtml;
    }

    tablebtn.tooltipster = function(option){
        var _option = {
            // contentCloning: true,
            interactive: true,
            trigger: 'click',
            side: 'left',
            theme: 'tooltipster-shadow'
        }
        _option = $.extend(true,_option,option);
        option.elem.tooltipster(_option);
    }
/*    function createBtn(data){
        vat btnHtml='<div class="layui-inline">';
        if(data.functions!=null && data.functions!=undefined){
            if(data.row!=undefined && data.row!=null){
                btnHtml += '';
            }else {
                btnHtml += '';
            }
        }
    }*/
    exports("tablebutton", tablebtn);
});
